ReflectionUtility.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using UnityEditor;
  6. using UnityEngine;
  7. namespace ExternPropertyAttributes.Editor
  8. {
  9. public static class ReflectionUtility
  10. {
  11. public static IEnumerable<FieldInfo> GetAllFields(object target, Func<FieldInfo, bool> predicate)
  12. {
  13. if (target == null)
  14. {
  15. // Debug.LogError("The target object is null. Check for missing scripts.");
  16. yield break;
  17. }
  18. List<Type> types = new List<Type>()
  19. {
  20. target.GetType()
  21. };
  22. while (types.Last().BaseType != null)
  23. {
  24. types.Add(types.Last().BaseType);
  25. }
  26. for (int i = types.Count - 1; i >= 0; i--)
  27. {
  28. IEnumerable<FieldInfo> fieldInfos = types[i]
  29. .GetFields(BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.DeclaredOnly)
  30. .Where(predicate);
  31. foreach (var fieldInfo in fieldInfos)
  32. {
  33. yield return fieldInfo;
  34. }
  35. }
  36. }
  37. public static IEnumerable<PropertyInfo> GetAllProperties(object target, Func<PropertyInfo, bool> predicate)
  38. {
  39. if (target == null)
  40. {
  41. Debug.LogError("The target object is null. Check for missing scripts.");
  42. yield break;
  43. }
  44. List<Type> types = new List<Type>()
  45. {
  46. target.GetType()
  47. };
  48. while (types.Last().BaseType != null)
  49. {
  50. types.Add(types.Last().BaseType);
  51. }
  52. for (int i = types.Count - 1; i >= 0; i--)
  53. {
  54. IEnumerable<PropertyInfo> propertyInfos = types[i]
  55. .GetProperties(BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.DeclaredOnly)
  56. .Where(predicate);
  57. foreach (var propertyInfo in propertyInfos)
  58. {
  59. yield return propertyInfo;
  60. }
  61. }
  62. }
  63. public static IEnumerable<MethodInfo> GetAllMethods(object target, Func<MethodInfo, bool> predicate)
  64. {
  65. if (target == null)
  66. {
  67. Debug.LogError("The target object is null. Check for missing scripts.");
  68. return null;
  69. }
  70. IEnumerable<MethodInfo> methodInfos = target.GetType()
  71. .GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public)
  72. .Where(predicate);
  73. return methodInfos;
  74. }
  75. public static FieldInfo GetField(object target, string fieldName)
  76. {
  77. return GetAllFields(target, f => f.Name.Equals(fieldName, StringComparison.InvariantCulture)).FirstOrDefault();
  78. }
  79. public static PropertyInfo GetProperty(object target, string propertyName)
  80. {
  81. return GetAllProperties(target, p => p.Name.Equals(propertyName, StringComparison.InvariantCulture)).FirstOrDefault();
  82. }
  83. public static MethodInfo GetMethod(object target, string methodName)
  84. {
  85. return GetAllMethods(target, m => m.Name.Equals(methodName, StringComparison.InvariantCulture)).FirstOrDefault();
  86. }
  87. public static Type GetListElementType(Type listType)
  88. {
  89. if (listType.IsGenericType)
  90. {
  91. return listType.GetGenericArguments()[0];
  92. }
  93. else
  94. {
  95. return listType.GetElementType();
  96. }
  97. }
  98. }
  99. }